package  com.lhs.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feign.user.UserFeignClient;
import com.lhs.Page.PageParam;
import com.lhs.Result.Result;
import com.lhs.VO.blog.ReviewVO;
import com.lhs.VO.user.UserInfoVO;
import com.lhs.entity.blog.blog_post;
import com.lhs.entity.blog.blog_review;
import com.lhs.mapper.blog_reviewMapper;
import com.lhs.service.blog_reviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class blog_reviewServiceImpl extends ServiceImpl<blog_reviewMapper, blog_review> implements blog_reviewService {

    @Autowired
    private blog_reviewMapper reviewMapper;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private com.lhs.mapper.blog_post_0Mapper postMapper; // 改名更规范

    @Override
    public Result<Page<ReviewVO>> getAllReviewBlog(PageParam pageParam) {
        // 构建分页对象
        Page<blog_review> page = pageParam.buildPage();

        // 分页审核列表
        IPage<blog_review> blogReviewPage = reviewMapper.selectPage(page, new QueryWrapper<>());

        // 批量拿到文章 ID，避免 N+1
        List<Long> postIds = blogReviewPage.getRecords().stream()
                .map(blog_review::getPostId)
                .distinct()
                .collect(Collectors.toList());

        // 一次性查出所有文章
        Map<Long, blog_post> postMap = postMapper.selectBatchIds(postIds).stream()
                .collect(Collectors.toMap(blog_post::getId, post -> post));

        // 批量拿用户信息（最好 user-service 提供批量接口）
        Set<Long> userIds = postMap.values().stream()
                .map(blog_post::getUserId)
                .collect(Collectors.toSet());

        Map<Long, UserInfoVO> userMap = userFeignClient.getUserInfoBatch(new ArrayList<>(userIds)).getData();

        // 封装 VO
        List<ReviewVO> reviewVOList = blogReviewPage.getRecords().stream().map(review -> {
            ReviewVO reviewVO = new ReviewVO();
            reviewVO.setId(review.getId());
            reviewVO.setPostId(review.getPostId());
            reviewVO.setStatus(review.getStatus());
            reviewVO.setCreateTime(review.getCreateTime());
            reviewVO.setUpdateTime(review.getUpdateTime());

            blog_post post = postMap.get(review.getPostId());
            if (post != null) {
                reviewVO.setTitle(post.getTitle());

                UserInfoVO user = userMap.get(post.getUserId());
                if (user != null) {
                    reviewVO.setUserId(user.getUserId());
                    reviewVO.setNickname(user.getNickname());
                }
            }

            return reviewVO;
        }).collect(Collectors.toList());

        // 构建分页返回
        Page<ReviewVO> reviewVOPage = new Page<>(blogReviewPage.getCurrent(), blogReviewPage.getSize(), blogReviewPage.getTotal());
        reviewVOPage.setRecords(reviewVOList);

        return Result.success(reviewVOPage);
    }
}
